package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.BuyerBean;
import com.project.bean.BuyerCarBean;
import com.project.bean.BuyerInfoBean;
import com.project.bean.ProductBean;
import com.project.mapper.*;
import com.project.service.IBuyerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
/**
 * @Title: BuyerServiceImpl
 * @Package: com.project.service.impl
 * @Description:
 * @Author: Liang-Xie
 */
@SuppressWarnings("all")
@Service
@Transactional
public class BuyerServiceImpl implements IBuyerService {
    @Autowired
    private IBuyerMapper buyerMapper;
    @Autowired
    private IBuyerCarMapper buyerCarMapper;
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IBuyerInfoMapper buyerInfoMapper;
    @Autowired
    private ISupplierMapper supplierMapper;


    @Override
    public IPage<BuyerBean> findByItem(Integer pageNO, String name,
                                       LocalDate startDate, LocalDate endDate) {

        return buyerMapper.findByItem(new Page<>(pageNO, 5), name, startDate, endDate);
    }

    @Override
    public BuyerBean findById(Integer buyerId) {
        BuyerBean buyerBean = buyerMapper.selectById(buyerId);
        buyerBean.setSupplierBean(supplierMapper.selectById(buyerBean.getSupplierId()));

        QueryWrapper<BuyerInfoBean> qw = new QueryWrapper<>();
        qw.eq("fk_buyerId", buyerId);
        List<BuyerInfoBean> beanList = buyerInfoMapper.selectList(qw);
        beanList.forEach(e -> {
            e.setProduct(productMapper.selectById(e.getProductId()));
        });
        buyerBean.setBuyerInfoList(beanList);
        return buyerBean;
    }


    @Override
    public void add(BuyerBean buyer, Integer userId) {
        //1.完成采购信息的添加
        buyerMapper.insert(buyer);
        //2.根据登陆用户的id 查询出采购车的集合
        QueryWrapper<BuyerCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId);
        List<BuyerCarBean> buyerCarList = buyerCarMapper.selectList(qw);
        for (BuyerCarBean buyerCar : buyerCarList) {
            //3.根据采购车的集合 添加采购明细
            BuyerInfoBean infoBean = new BuyerInfoBean();
            infoBean.setBuyerId(buyer.getId());
            infoBean.setNum(buyerCar.getNum());
            infoBean.setProductId(buyerCar.getProductId());
            ProductBean product = productMapper.selectById(buyerCar.getProductId());
            infoBean.setBuyMoney(buyerCar.getMoney());
            buyerInfoMapper.insert(infoBean);
            //4.修改库存商品的最新采购价格 和库存数量进行累加
            product.setBuyPrice(buyerCar.getMoney());
            product.setNum(product.getNum() + buyerCar.getNum());
            productMapper.updateById(product);
        }
        // 5.采购信息添加成功后 清空该用户购物车信息
        buyerCarMapper.delete(qw);
    }
}
